package com.spring.provideronetest.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @auther zhangxiangtao
 * @date 2021/3/8
 */
@RestController
@RequestMapping("/user/data")
public class UserCenterController {

	@Autowired
	private RestTemplate restTemplate;

	@Autowired
	private DiscoveryClient discoveryClient;

	@GetMapping("/getTodayStatistic/{id}")
	@HystrixCommand(
			fallbackMethod = "getTodayStatisticFallback",// 服务降级方法

			// 使用commandProperties 可以配置熔断的一些细节信息
			commandProperties = {

					// 类似kv形式
					//这里这个参数意思是熔断超时时间2s，表示过了多长时间还没结束就进行熔断
					@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000")
			}
	)
	public String getToday(@PathVariable("id") String id){
//		List<ServiceInstance> instances = discoveryClient.getInstances("provider-user-order");
//
//		ServiceInstance instanceInfo = instances.get(0);
//
//		String ip = instanceInfo.getHost();
//
//		int port = instanceInfo.getPort();
		String url  ="http://provider-user-order/user/userList/";
//		String url  ="http://"+ip+":"+port+"/user/userList/";
		return restTemplate.getForObject(url,String.class);
	}

	// 服务降级方法 ，这里参数与返回值需要原方法保持一直
	public String getTodayStatisticFallback(String id){
		return "降级";
	}

}
